package com.itheima.reggie.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.PageBean;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

import java.util.List;

/**
 * 员工管理业务层
 * @author Asin
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;


    /**
     * 员工登录
     *
     * @param employee
     * @return
     */
    @Override
    public R<Employee> login(Employee employee) {
        Employee loginEmployee = employeeMapper.findEmployeeByUserName(employee);
        if (loginEmployee == null){
            //登录失败，返回错误信息
            return R.error("用户名不存在");
        }
        //如果用户存在，那么获取到的loginEmployee对象中存在md5加密的密码数据
        //只需要加密前端传输回来的密码跟loginEmployee中的密码对比一下，就可以查看密码是否正确
        String md5Password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());//加密
        if (!md5Password.equals(loginEmployee.getPassword())){
            //不相同，响应密码错误
            return R.error("密码错误");
        }
        //用户存在，密码正确，判断用户是否为被封禁用户
        if (loginEmployee.getStatus() == 0){
            return R.error("账号被禁用了");
        }

        //到此仍然满足，则登录成功
        return R.success(loginEmployee);


    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public R<PageBean> findByPage(int page, int pageSize,String name) {

        PageHelper.startPage(page,pageSize);

        List<Employee> employees = employeeMapper.findAll(name);

        if (employees!=null){


            PageInfo<Employee> pageInfo = new PageInfo<Employee>(employees);
            List<Employee> list = pageInfo.getList();
            long total = pageInfo.getTotal();
            return R.success(new PageBean(list, total,pageSize, page));
        }else {
            return R.error("查询失败");
        }

    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @Override
    public R<String> addEmployee(Employee employee) {

        if (employee.getUsername()!=null){
            Employee dbEmployee = employeeMapper.findEmployeeByUserName(employee);
            if (dbEmployee!=null){
                return R.error("用户名已存在");
            }
        }

        //默认密码123456
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        //设置默认账号状态
        employee.setStatus(1);
        //设置新增和更新时间为当前时间

        LocalDateTime now = LocalDateTime.now();
        employee.setCreateTime(now);
        employee.setUpdateTime(now);

        int result = employeeMapper.addEmployee(employee);
        if (result>0){
            return R.success("添加成功");
        }else {
            return R.error("添加失败");
        }

    }

    /**
     * 修改员工信息
     * @param employee
     * @return
     */
    @Override
    public R<String> updateEmployee(Employee employee) {
        if (employee.getUsername()!=null){
            Employee dbEmployee = employeeMapper.findEmployeeByUserName(employee);
            if (dbEmployee!=null){
                return R.error("用户名已存在");
            }
        }
        int result = employeeMapper.updateEmployee(employee);
        if (result>0){
            return R.success("修改成功!");
        }
        return R.error("修改失败！");

    }

    /**
     * 通过id获取员工信息
     *
     * @param id
     * @return
     */
    @Override
    public R<Employee> findEmployeeById(int id) {
        Employee employee= employeeMapper.findEmployeeById(id);
        if (employee!=null){
            return R.success(employee);
        }
        return R.error("查询失败");
    }



}
